<%
local api = require "luci.model.cbi.passwall.api.api"
local dsp = require "luci.dispatcher"

local gfwlist_version = api.uci_get_type("global_rules", "gfwlist_version")
local chnroute_version = api.uci_get_type("global_rules", "chnroute_version")
local chnlist_version = api.uci_get_type("global_rules", "chnlist_version")

local gfwlist_update = api.uci_get_type("global_rules", "gfwlist_update", "1") == "1" and "checked='checked'" or ""
local chnroute_update = api.uci_get_type("global_rules", "chnroute_update", "1") == "1" and "checked='checked'" or ""
local chnlist_update = api.uci_get_type("global_rules", "chnlist_update", "1") == "1" and "checked='checked'" or ""
-%>

<script type="text/javascript">
	//<![CDATA[
	function update_rules(btn) {
		btn.disabled = true;
		btn.value = '<%:Updating...%>';
		var table = document.getElementById('_rule_fieldset');
		var domList = table.getElementsByTagName('input');
		var checkBoxList = [];
		var len = domList.length;
		while(len--) {
			var dom = domList[len];　　
			if(dom.type == 'checkbox' && dom.checked) {　　
				checkBoxList.push(dom.name);　　
			}
		}
		XHR.get('<%=dsp.build_url("admin/vpn/passwall/update_rules")%>', {
				update: checkBoxList
			},
			function(x, data) {
				if(x && x.status == 200) {
					window.location.href = '<%=dsp.build_url("admin/vpn/passwall/log")%>';
				} else {
					alert("<%:Error%>");
					btn.disabled = false;
					btn.value = '<%:Manually update%>';
				}
			}
		);
	}
	//]]>
</script>
<fieldset class="cbi-section">
	<fieldset class="cbi-section" id="_rule_fieldset">
		<div class="cbi-value">
			<label class="cbi-value-title">gfwlist
				<%:Version%>
			</label>
			<div class="cbi-value-field">
				<div class="cbi-value-description">
					<span>【 <%=gfwlist_version%> 】</span>
					<input type="checkbox" name="gfwlist_update" value="1" <%=gfwlist_update%> />
				</div>
			</div>
		</div>

		<div class="cbi-value">
			<label class="cbi-value-title">chnroute
				<%:Version%>
			</label>
			<div class="cbi-value-field">
				<div class="cbi-value-description">
					<span>【 <%=chnroute_version%> 】</span>
					<input type="checkbox" name="chnroute_update" value="1" <%=chnroute_update%> />
				</div>
			</div>
		</div>
		
		<div class="cbi-value">
			<label class="cbi-value-title">chnlist
				<%:Version%>
			</label>
			<div class="cbi-value-field">
				<div class="cbi-value-description">
					<span>【 <%=chnlist_version%> 】</span>
					<input type="checkbox" name="chnlist_update" value="1" <%=chnlist_update%> />
				</div>
			</div>
		</div>

		<div class="cbi-value">
			<label class="cbi-value-title">
				<%:Manually update%>
			</label>
			<div class="cbi-value-field">
				<input class="cbi-button cbi-input-apply" type="button" id="update_rules_btn" onclick="update_rules(this)" value="<%:Manually update%>" />
			</div>
		</div>
		
	</fieldset>
</fieldset>